﻿using System;
using System.Runtime.Serialization;
using System.ServiceModel;

namespace MultiViewer.Network
{
    [DataContract]
    public class Response
    {
        protected object success;
        protected Exception fault;

        public Response(object success)
        {
            this.success = success;
            this.fault = null;
        }

        public Response(Exception fault)
        {
            this.fault = fault;
            this.success = null;
        }

        [DataMember]
        public object Success
        {
            get { return success; }
            set { success = value; }
        }

        [DataMember]
        public Exception Fault
        {
            get { return fault; }
            set { fault = value; }
        }

        public bool IsFault
        {
            get
            {
                return (fault != null);
            }
        }
    }

    public class Response<T,E> : Response where E : Exception
    {
        public Response(T success) : base(success)
        {
            this.success = success;
            this.fault = null;
        }

        public Response(E fault) : base(fault)
        {
            this.fault = fault;
            this.success = null;
        }

        public new T Success
        {
            get
            {
                if (fault != null)
                    throw new InvalidOperationException("Remote node responded with fault");
                return (T)success;
            }
        }

        public new E Fault
        {
            get
            {
                if (fault == null)
                    throw new InvalidOperationException("Remote node responded with success");
                return (E)fault;
            }
        }
    }
}